package com.internetCafes.spms.web.extend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.AIRecognize.recognizer.impl.Pic2WordAIRecognizer;
import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.common.module.excel.ExcelUtils;
import com.internetCafes.spms.common.module.excel.po.EntCertiQualiExcel;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.core.utils.DateUtils;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.extend.model.EntCertiIso;
import com.internetCafes.spms.web.extend.model.EntCertiQuali;
import com.internetCafes.spms.web.extend.model.EntCertiQualiConf;
import com.internetCafes.spms.web.extend.model.EntCertiQualiRenewal;
import com.internetCafes.spms.web.extend.service.IEntCertiIsoService;
import com.internetCafes.spms.web.extend.service.IEntCertiQualiConfService;
import com.internetCafes.spms.web.extend.service.IEntCertiQualiRenewalService;
import com.internetCafes.spms.web.extend.service.IEntCertiQualiService;
import com.internetCafes.spms.web.extend.vo.EntCertiQualiVo;
import com.internetCafes.spms.web.sys.model.ConfInfo;
import com.internetCafes.spms.web.sys.model.ResourceInfo;
import com.internetCafes.spms.web.sys.service.ConfInfoService;
import com.internetCafes.spms.web.sys.service.ResourceInfoService;
import com.internetCafes.spms.web.sys.vo.ConfInfoVo;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LinYe
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/cert/entQuali")
public class EntCertiQualiController extends BaseWebController {

    @Autowired
    IEntCertiQualiService entCertiQualiService;

    @Autowired
    Pic2WordAIRecognizer pic2WordAIRecognizer;

    @Autowired
    ResourceInfoService resourceInfoService;

    @Autowired
    IEntCertiQualiRenewalService entCertiQualiRenewalService;

    @Autowired
    ConfInfoService confInfoService;

    @Autowired
    IEntCertiQualiConfService entCertiQualiConfService;

    @Autowired
    IEntCertiIsoService entCertiIsoService;

    @SysLog("保存资质证书")
    @PostMapping("/save")
    @RequiresPermissions("certificate:certificateInfo:save")
    public R save(@RequestBody EntCertiQualiVo entCertiQualiVo) {
        entCertiQualiVo.setStatus(CommonConstants.CommonStatus.DISABLE);
        QueryWrapper<EntCertiQuali> entCertiQualiWrapper = new QueryWrapper<>();
        entCertiQualiWrapper.eq("code", entCertiQualiVo.getCode());
        int count = entCertiQualiService.count(entCertiQualiWrapper);
        if (count > 0) {
            return R.error("证书已存在");
        }
        entCertiQualiService.save(entCertiQualiVo);
        if (StringUtils.isNotBlank(entCertiQualiVo.getCertiPic())) {
            ResourceInfo r = new ResourceInfo(ShiroUtils.getUserEntity().getId(), CommonConstants.RESOURCE_PIC_ENT_QUALI, entCertiQualiVo.getId(), "pic" + File.separator + entCertiQualiVo.getCertiPic());
            resourceInfoService.save(r);
        }
        if (CollectionUtil.isNotEmpty(entCertiQualiVo.getQualifications()) && CollectionUtil.isNotEmpty(entCertiQualiVo.getLevels())) {
            List<Long> qualifications = entCertiQualiVo.getQualifications();
            List<Integer> levels = entCertiQualiVo.getLevels();
            EntCertiQualiConf entCertiQualiConf;
            for (int i = 0; i < qualifications.size(); i++) {
                Long qualification = qualifications.get(i);
                Integer level = levels.get(i);
                entCertiQualiConf = new EntCertiQualiConf();
                entCertiQualiConf.setCertificateId(entCertiQualiVo.getId());
                entCertiQualiConf.setConfId(qualification);
                entCertiQualiConf.setLevel(level);
                entCertiQualiConfService.save(entCertiQualiConf);
            }
        }
        return R.ok();
    }

    @SysLog("作废资质证书")
    @PostMapping("/delete")
    @RequiresPermissions("certificate:certificateInfo:delete")
    public R delete(@RequestBody Map<String, Object> params) {
        EntCertiQuali entCertiQuali = new EntCertiQuali();
        entCertiQuali.setId(Long.valueOf(String.valueOf(params.get("id"))));
        entCertiQuali.setStatus(CommonConstants.CommonStatus.DELETED);
        entCertiQuali.setUnableRemark((String) params.get("unableRemark"));
        if (entCertiQualiService.updateById(entCertiQuali)) {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("certificate_id", entCertiQuali.getId());
            updateWrapper.set("status", CommonConstants.CommonStatus.DELETED);
            entCertiQualiConfService.update(updateWrapper);
            return R.ok();
        } else {
            return R.error("您要删除的证书不存在");
        }
    }

    @SysLog("更新资质证书")
    @PostMapping("/update")
    @RequiresPermissions("certificate:certificateInfo:update")
    public R update(@RequestBody EntCertiQualiVo entCertiQualiVo) {
        if (entCertiQualiService.updateById(entCertiQualiVo)) {
            Map<String, Object> params = new HashMap<>();
            if (entCertiQualiVo.getCertiPic() != null) {
                params.put("moduleCode", CommonConstants.RESOURCE_PIC_ENT_QUALI);
                params.put("moduleId", entCertiQualiVo.getId());
                resourceInfoService.deleteByModuleAndUserId(params);
                if (!"".equals(entCertiQualiVo.getCertiPic())) {
                    ResourceInfo r = new ResourceInfo(ShiroUtils.getUserEntity().getId(), CommonConstants.RESOURCE_PIC_ENT_QUALI, entCertiQualiVo.getId(), "pic" + File.separator + entCertiQualiVo.getCertiPic());
                    resourceInfoService.save(r);
                }
            }

            if (CollectionUtil.isNotEmpty(entCertiQualiVo.getQualifications()) && CollectionUtil.isNotEmpty(entCertiQualiVo.getLevels())) {
                params = new HashMap<>();
                params.put("certificate_id", entCertiQualiVo.getId());
                entCertiQualiConfService.removeByMap(params);
                List<Long> qualifications = entCertiQualiVo.getQualifications();
                List<Integer> levels = entCertiQualiVo.getLevels();
                EntCertiQualiConf entCertiQualiConf;
                for (int i = 0; i < qualifications.size(); i++) {
                    Long qualification = qualifications.get(i);
                    Integer level = levels.get(i);
                    entCertiQualiConf = new EntCertiQualiConf();
                    entCertiQualiConf.setCertificateId(entCertiQualiVo.getId());
                    entCertiQualiConf.setConfId(qualification);
                    entCertiQualiConf.setLevel(level);
                    entCertiQualiConfService.save(entCertiQualiConf);
                }
            }
            return R.ok();
        } else {
            return R.error("您要更新的证书不存在");
        }
    }

    @GetMapping("/getDetail/{id}")
    @RequiresPermissions("certificate:certificateInfo:info")
    public R get(@PathVariable("id") Long id) {
        EntCertiQuali entCertiQuali = entCertiQualiService.getById(id);
        if (entCertiQuali != null) {
            Map<String, Object> params = new HashMap<>();
            params.put("moduleCode", CommonConstants.RESOURCE_PIC_ENT_QUALI);
            params.put("moduleId", id);
            List<ResourceInfo> resourceInfoList = resourceInfoService.query(params);
            EntCertiQualiVo entCertiQualiVo = new EntCertiQualiVo();
            BeanUtils.copyProperties(entCertiQuali, entCertiQualiVo);
            if (CollectionUtil.isNotEmpty(resourceInfoList)) {
                entCertiQualiVo.setCertiPic(resourceInfoList.get(0).getPath());
            }
            List<ConfInfo> confInfoList = new ArrayList<>();
            List<List<Integer>> confLevels = new ArrayList<>();
            QueryWrapper<EntCertiQualiConf> entCertiQualiConfWrapper = new QueryWrapper<>();
            entCertiQualiConfWrapper.eq("certificate_id", id);
            List<EntCertiQualiConf> list = entCertiQualiConfService.list(entCertiQualiConfWrapper);
            if (CollectionUtil.isNotEmpty(list)) {
                List<Long> qualifications = new ArrayList<>();
                List<Integer> levels = new ArrayList<>();
                List<String> qualificationDetails = new ArrayList<>();
                for (EntCertiQualiConf entCertiQualiConf : list) {
                    qualifications.add(entCertiQualiConf.getConfId());
                    levels.add(entCertiQualiConf.getLevel());
                    ConfInfo confInfo = confInfoService.findById(entCertiQualiConf.getConfId());
                    ConfInfoVo confInfoVo = new ConfInfoVo();
                    BeanUtil.copyProperties(confInfo, confInfoVo);
                    confInfoVo.setLevelList(levels);
                    confInfoList.add(confInfoVo);
                    qualificationDetails.add(confInfo.getValue() + CommonConstants.QualificationLevels.getDescr(entCertiQualiConf.getLevel()));
                }
                for (ConfInfo confInfo : confInfoList) {
                    confLevels.add(CommonConstants.QualificationLevels.getLevels(confInfo.getName()));
                }
                entCertiQualiVo.setConfInfoList(confInfoList);
                entCertiQualiVo.setConfLevels(confLevels);
                entCertiQualiVo.setQualifications(qualifications);
                entCertiQualiVo.setLevels(levels);
                entCertiQualiVo.setQualificationDetails(qualificationDetails);
            }

            return R.ok().put("data", entCertiQualiVo);
        } else {
            return R.error("您要查询的证书不存在");
        }
    }

    @PostMapping("/paging")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R paging(@RequestBody EntCertiQualiVo entCertiQualiVo) {
        if (entCertiQualiVo.getStatus() == null) {
            entCertiQualiVo.setStatus(CommonConstants.CommonStatus.DISABLE);
        }
        Page<EntCertiQuali> page = new Page<>(entCertiQualiVo.getPageNum(), entCertiQualiVo.getPageSize());
        QueryWrapper<EntCertiQuali> entCertiQualiQueryWrapper = new QueryWrapper<>(entCertiQualiVo);
        if (entCertiQualiVo.getName() != null) {
            entCertiQualiQueryWrapper.like("name", entCertiQualiVo.getName());
        }
        if (entCertiQualiVo.getOrderBy() != null) {
            if ("warningCount".equals(entCertiQualiVo.getOrderBy())) {
                entCertiQualiQueryWrapper.orderBy(true, entCertiQualiVo.isAscFlag(), "long_term")
                        .orderBy(true, entCertiQualiVo.isAscFlag(), "expire_date");
            } else {
                entCertiQualiQueryWrapper.orderBy(entCertiQualiVo.getOrderBy() != null,
                        entCertiQualiVo.isAscFlag(),
                        com.internetCafes.spms.common.utils.StringUtils.HumpToUnderline(entCertiQualiVo.getOrderBy()));
            }
        }
        if (entCertiQualiVo.getWechatParam() != null) {
            entCertiQualiQueryWrapper.and(wrapper -> wrapper.like("name", entCertiQualiVo.getWechatParam()).or().like("code", entCertiQualiVo.getWechatParam()).or().like("release_organ", entCertiQualiVo.getWechatParam()));
        }
        entCertiQualiQueryWrapper.orderBy(true, false, "id");
        if (entCertiQualiVo.getValidityType() != null) {
            switch (entCertiQualiVo.getValidityType()) {
                case -1:
                    entCertiQualiQueryWrapper.lt("expire_date", new Date());
                    break;
                case 0:
                    entCertiQualiQueryWrapper.and(wrapper -> wrapper.gt("expire_date", new Date()));
                    entCertiQualiQueryWrapper.lt("expire_date", entCertiQualiVo.getDays() != null ? DateUtils.getBeAfDay(Integer.parseInt(entCertiQualiVo.getDays())) : entCertiQualiVo.getMonths() != null ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiQualiVo.getMonths())) : null);
                    break;
                case 2:
                    entCertiQualiQueryWrapper.and(wrapper -> wrapper.lt("expire_date", new Date()).or().gt("expire_date", new Date()).lt("expire_date", entCertiQualiVo.getDays() != null ? DateUtils.getBeAfDay(Integer.parseInt(entCertiQualiVo.getDays())) : entCertiQualiVo.getMonths() != null ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiQualiVo.getMonths())) : null));

            }
        }
        IPage<EntCertiQuali> entCertiQualiIPage = entCertiQualiService.page(page, entCertiQualiQueryWrapper);
        List<EntCertiQuali> records = entCertiQualiIPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            EntCertiQuali entCertiQuali = records.get(i);
            Map<String, Object> params = new HashMap<>();
            params.put("moduleCode", CommonConstants.RESOURCE_PIC_ENT_QUALI);
            params.put("moduleId", entCertiQuali.getId());
            List<ResourceInfo> resourceInfoList = resourceInfoService.query(params);
            EntCertiQualiVo certiQualiVo = new EntCertiQualiVo();
            BeanUtils.copyProperties(entCertiQuali, certiQualiVo);
            if (CollectionUtil.isNotEmpty(resourceInfoList)) {
                certiQualiVo.setCertiPic(resourceInfoList.get(0).getPath());
            }
            List<ConfInfo> confInfoList = new ArrayList<>();
            List<List<Integer>> confLevels = new ArrayList<>();
            QueryWrapper<EntCertiQualiConf> entCertiQualiConfWrapper = new QueryWrapper<>();
            entCertiQualiConfWrapper.eq("certificate_id", entCertiQuali.getId());
            List<EntCertiQualiConf> entCertiQualiConfList = entCertiQualiConfService.list(entCertiQualiConfWrapper);
            if (CollectionUtil.isNotEmpty(entCertiQualiConfList)) {
                List<Long> qualifications = new ArrayList<>();
                List<Integer> levels = new ArrayList<>();
                List<String> qualificationDetails = new ArrayList<>();
                for (EntCertiQualiConf entCertiQualiConf : entCertiQualiConfList) {
                    qualifications.add(entCertiQualiConf.getConfId());
                    levels.add(entCertiQualiConf.getLevel());
                    ConfInfo confInfo = confInfoService.findById(entCertiQualiConf.getConfId());
                    ConfInfoVo confInfoVo = new ConfInfoVo();
                    BeanUtil.copyProperties(confInfo, confInfoVo);
                    confInfoVo.setLevelList(levels);
                    confInfoList.add(confInfoVo);
                    qualificationDetails.add(confInfo.getValue() + CommonConstants.QualificationLevels.getDescr(entCertiQualiConf.getLevel()));
                }
                for (ConfInfo confInfo : confInfoList) {
                    confLevels.add(CommonConstants.QualificationLevels.getLevels(confInfo.getName()));
                }
                certiQualiVo.setConfInfoList(confInfoList);
                certiQualiVo.setConfLevels(confLevels);
                certiQualiVo.setQualifications(qualifications);
                certiQualiVo.setLevels(levels);
                certiQualiVo.setQualificationDetails(qualificationDetails);
            }
            records.set(i, certiQualiVo);
            entCertiQualiIPage.setRecords(records);
        }
        return R.ok().put("data", entCertiQualiIPage);
    }

    @PostMapping("/list")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R list(@RequestBody EntCertiQualiVo entCertiQualiVo) {
        entCertiQualiVo.setStatus(CommonConstants.CommonStatus.DISABLE);
        QueryWrapper<EntCertiQuali> entCertiQualiQueryWrapper = new QueryWrapper<>(entCertiQualiVo);
        entCertiQualiQueryWrapper.orderBy(entCertiQualiVo.getOrderBy() != null, entCertiQualiVo.isAscFlag(), entCertiQualiVo.getOrderBy());
        List<EntCertiQuali> entCertiQualiList = entCertiQualiService.list(entCertiQualiQueryWrapper);
        for (int i = 0; i < entCertiQualiList.size(); i++) {
            EntCertiQuali entCertiQuali = entCertiQualiList.get(i);
            Map<String, Object> params = new HashMap<>();
            params.put("moduleCode", CommonConstants.RESOURCE_PIC_ENT_QUALI);
            params.put("moduleId", entCertiQuali.getId());
            List<ResourceInfo> resourceInfoList = resourceInfoService.query(params);
            EntCertiQualiVo certiQualiVo = new EntCertiQualiVo();
            BeanUtils.copyProperties(entCertiQuali, certiQualiVo);
            if (CollectionUtil.isNotEmpty(resourceInfoList)) {
                certiQualiVo.setCertiPic(resourceInfoList.get(0).getPath());
            }
            QueryWrapper<EntCertiQualiConf> entCertiQualiConfWrapper = new QueryWrapper<>();
            entCertiQualiConfWrapper.eq("certificate_id", entCertiQuali.getId());
            List<EntCertiQualiConf> entCertiQualiConfList = entCertiQualiConfService.list(entCertiQualiConfWrapper);
            if (CollectionUtil.isNotEmpty(entCertiQualiConfList)) {
                List<Long> qualifications = new ArrayList<>();
                List<Integer> levels = new ArrayList<>();
                List<String> qualificationDetails = new ArrayList<>();
                for (EntCertiQualiConf entCertiQualiConf : entCertiQualiConfList) {
                    qualifications.add(entCertiQualiConf.getConfId());
                    levels.add(entCertiQualiConf.getLevel());
                    params = new HashMap<>();
                    params.put("id", entCertiQualiConf.getConfId());
                    List<ConfInfo> confInfoList = confInfoService.query(params);
                    qualificationDetails.add(confInfoList.get(0).getValue() + CommonConstants.QualificationLevels.getDescr(entCertiQualiConf.getLevel()));
                }
                certiQualiVo.setQualifications(qualifications);
                certiQualiVo.setLevels(levels);
                certiQualiVo.setQualificationDetails(qualificationDetails);
            }
            entCertiQualiList.set(i, certiQualiVo);
        }
        return R.ok().put("data", entCertiQualiList);
    }

    @PostMapping("/export")
    @RequiresPermissions("certificate:certificateInfo:list")
    public void export(@RequestBody EntCertiQualiVo entCertiQualiVo, HttpServletResponse response) {
        entCertiQualiVo.setStatus(CommonConstants.CommonStatus.DISABLE);
        QueryWrapper<EntCertiQuali> entCertiQualiWrapper = new QueryWrapper<>(entCertiQualiVo);
        entCertiQualiWrapper.orderBy(entCertiQualiVo.getOrderBy() != null, entCertiQualiVo.isAscFlag(), entCertiQualiVo.getOrderBy());
        List<EntCertiQuali> list = entCertiQualiService.list(entCertiQualiWrapper);
        try {
            if (list != null && list.size() > 0) {
                List<EntCertiQualiExcel> orderInfoExcelList = new ArrayList<>();
                EntCertiQualiExcel entCertiQualiExcel;
                for (EntCertiQuali entCertiQuali : list) {
                    entCertiQualiExcel = new EntCertiQualiExcel();
                    BeanUtils.copyProperties(entCertiQuali, entCertiQualiExcel);
                    QueryWrapper<EntCertiQualiRenewal> entCertiQualiRenewalWrapper = new QueryWrapper<>();
                    entCertiQualiRenewalWrapper.eq("certificate_id", entCertiQuali.getId());
                    List<EntCertiQualiRenewal> entCertiQualiRenewals = entCertiQualiRenewalService.list(entCertiQualiRenewalWrapper);
                    if (CollectionUtils.isNotEmpty(entCertiQualiRenewals)) {
                        for (EntCertiQualiRenewal renewal : entCertiQualiRenewals) {
                            entCertiQualiExcel.setRenewals(renewal.toString());
                        }
                    }
                    QueryWrapper<EntCertiQualiConf> entCertiQualiConfQueryWrapper = new QueryWrapper<>();
                    entCertiQualiConfQueryWrapper.eq("certificate_id", entCertiQuali.getId());
                    List<EntCertiQualiConf> confList = entCertiQualiConfService.list(entCertiQualiConfQueryWrapper);
                    if (CollectionUtil.isNotEmpty(confList)) {
                        StringBuilder qualifications = new StringBuilder();
                        for (EntCertiQualiConf entCertiQualiConf : confList) {
                            ConfInfo confInfo = confInfoService.findById(entCertiQualiConf.getConfId());
                            qualifications.append(confInfo.getValue()).append(CommonConstants.QualificationLevels.getDescr(entCertiQualiConf.getLevel())).append(",");

                        }
                        qualifications = new StringBuilder(qualifications.substring(0, qualifications.length() - 1));
                        entCertiQualiExcel.setQualificationDetails(qualifications.toString());
                    }
                    QueryWrapper<EntCertiQualiConf> entCertiQualiConfWrapper = new QueryWrapper<>();
                    entCertiQualiConfWrapper.eq("certificate_id", entCertiQualiVo.getId());
                    List<EntCertiQualiConf> entCertiQualiConfList = entCertiQualiConfService.list(entCertiQualiConfWrapper);
                    if (CollectionUtil.isNotEmpty(entCertiQualiConfList)) {
                        StringBuilder qualification;
                        for (EntCertiQualiConf entCertiQualiConf : entCertiQualiConfList) {
                            qualification = new StringBuilder();
                            qualification.append(entCertiQualiConf.getConfId());
                            Integer level = entCertiQualiConf.getLevel();
                            String descr = CommonConstants.QualificationLevels.getDescr(level);
                            qualification.append(descr);
                        }

                    }
                    orderInfoExcelList.add(entCertiQualiExcel);
                }
                ExcelUtils.exportExcel(orderInfoExcelList, "资质证书信息统计", "资质证书信息统计", EntCertiQualiExcel.class, "资质证书信息统计.xls", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/count")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R count(@ModelAttribute EntCertiQuali entCertiQuali) {
        entCertiQuali.setStatus(CommonConstants.CommonStatus.DISABLE);
        QueryWrapper<EntCertiQuali> entCertiQualiQueryWrapper = new QueryWrapper<>(entCertiQuali);
        int count = entCertiQualiService.count(entCertiQualiQueryWrapper);
        return R.ok().put("count", count);
    }

    @GetMapping("/qualiLevelsCount")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R countQualiLevels() {
        QueryWrapper<EntCertiQualiConf> entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("status", CommonConstants.CommonStatus.DISABLE);
        entCertiQualiConfQueryWrapper.eq("level", CommonConstants.QualificationLevels.SPECIAL);
        int special = entCertiQualiConfService.count(entCertiQualiConfQueryWrapper);
        entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("level", CommonConstants.QualificationLevels.FIRST);
        int first = entCertiQualiConfService.count(entCertiQualiConfQueryWrapper);
        entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("level", CommonConstants.QualificationLevels.SECOND);
        int second = entCertiQualiConfService.count(entCertiQualiConfQueryWrapper);
        entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("level", CommonConstants.QualificationLevels.THIRD);
        int third = entCertiQualiConfService.count(entCertiQualiConfQueryWrapper);
        entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("level", CommonConstants.QualificationLevels.NO_CLASS);
        int noClass = entCertiQualiConfService.count(entCertiQualiConfQueryWrapper);
        return R.ok().put("special", special)
                .put("first", first)
                .put("second", second)
                .put("third", third)
                .put("noClass", noClass);
    }

    @GetMapping("/qualiByLevel/{level}")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R QualiByLevels(@PathVariable Integer level) {
        List<ConfInfo> qualifications = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        QueryWrapper<EntCertiQualiConf> entCertiQualiConfQueryWrapper = new QueryWrapper<>();
        entCertiQualiConfQueryWrapper.eq("level", level);
        List<EntCertiQualiConf> list = entCertiQualiConfService.list(entCertiQualiConfQueryWrapper);
        params.put("type", CommonConstants.CONF_QUALIFICATION_GRADE_TYPE);
        List<ConfInfo> confInfoList = confInfoService.query(params);
        for (EntCertiQualiConf entCertiQualiConf : list) {
            for (ConfInfo confInfo : confInfoList) {
                if (entCertiQualiConf.getConfId().equals(confInfo.getId())) {
                    if (!qualifications.contains(confInfo.getValue())) {
                        qualifications.add(confInfo);
                    }
                }
            }
        }
        return R.ok().put("qualifications", qualifications);
    }

    @GetMapping("/warningCount")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R countWarning() {
        QueryWrapper<EntCertiQuali> entCertiQualiQueryWrapper = new QueryWrapper();
        entCertiQualiQueryWrapper.gt("expire_date", new Date());
        entCertiQualiQueryWrapper.lt("expire_date", DateUtils.getBeAfMonth(3));
        int qualiThreeCount = entCertiQualiService.count(entCertiQualiQueryWrapper);
        entCertiQualiQueryWrapper.lt("expire_date", DateUtils.getBeAfMonth(1));
        int qualiOneCount = entCertiQualiService.count(entCertiQualiQueryWrapper);
        entCertiQualiQueryWrapper = new QueryWrapper();
        entCertiQualiQueryWrapper.lt("expire_date", new Date());
        int qualiCount = entCertiQualiService.count(entCertiQualiQueryWrapper);

        QueryWrapper<EntCertiIso> entCertiIsoQueryWrapper = new QueryWrapper();
        entCertiIsoQueryWrapper.gt("expire_date", new Date());
        entCertiIsoQueryWrapper.lt("expire_date", DateUtils.getBeAfMonth(3));
        int isoThreeCount = entCertiIsoService.count(entCertiIsoQueryWrapper);
        entCertiIsoQueryWrapper.lt("expire_date", DateUtils.getBeAfMonth(1));
        int isoOneCount = entCertiIsoService.count(entCertiIsoQueryWrapper);
        entCertiIsoQueryWrapper = new QueryWrapper();
        entCertiIsoQueryWrapper.lt("expire_date", new Date());
        int isoCount = entCertiIsoService.count(entCertiIsoQueryWrapper);
        return R.ok().put("qualiThreeCount", qualiThreeCount).put("qualiOneCount", qualiOneCount).put("qualiCount", qualiCount)
                .put("isoThreeCount", isoThreeCount).put("isoOneCount", isoOneCount).put("isoCount", isoCount);
    }

}
